<?php
include('lib/ftree_utils.php');
require_once "HTML/Template/IT.php";                                                                                                              


function LoadEvents ($dyear, $dmonth, $field, $label)
{
	global $monEvts;
	
	// CONNECT TO DB
	
	$link = mysql_connect("localhost", "ftreeweb", "ftree_web")
	   or die("Could not connect : " . mysql_error());
	mysql_select_db("ftree") or die("Could not select database");

// GET PERSON 
/*        
$query = "SELECT * FROM person WHERE `id` = $id";
$result = mysql_query($query) or die("Query failed : " . mysql_error());
$idData = mysql_fetch_array($result, MYSQL_ASSOC);
*/

	$query = ' SELECT id, first, last, '
		//. '   ( YEAR( CURDATE( ) ) - YEAR( '.$field.' ) ) - ( RIGHT( CURDATE( ) , 5 ) < RIGHT( '.$field.', 5 ) ) AS age, '
		. '   DAYOFMONTH( '.$field.' ) AS eday, '
		. '   YEAR( '.$field.' ) AS eyear'
		. ' FROM `person` '
		. ' WHERE MONTH( '.$field.' ) = \'' . $dmonth . '\''
		. ' ORDER BY `eday` ASC';

	//print ("query=$query\n");
	$result = mysql_query($query) or die("Query failed : " . mysql_error());

	while ($dbEvts = mysql_fetch_array($result, MYSQL_ASSOC)) {

		$eid = $dbEvts['id'];
		$efirst = $dbEvts['first'];
		$elast = $dbEvts['last'];
		$eday = $dbEvts['eday'];
		$eyear = $dbEvts['eyear'];
		$eage = ($dyear - $eyear);

		//$mEvts[{$dbEvts['eday']}] .= "<a href=\"person.php?id={$dbEvts['id']}\">{$dbEvts['first']} {$dbEvts['last']}</a> {$dbEvts['eyear']} ({$dbEvts['age']})<br>";
		$monEvts[$eday] .= "      <a href=\"person.php?id=$eid\">".htmlentities($efirst,NULL,"UTF-8") ." ". htmlentities($elast,NULL,"UTF-8")."</a> $label$eyear ($eage)<br>\n";

	}

	mysql_free_result($result);

}

function DrawCal ($dyear, $dmonth, $now_mon, $now_day)
{
	global $monEvts, $id;

	$pmonth = $dmonth - 1; if ($pmonth ==  0) { $pmonth = 12; }
	$nmonth = $dmonth + 1; if ($nmonth == 13) { $nmonth =  1; }
	$html = "";
	$cwid = "14%";
	$twid = "100%";
	$rhei = 100;
	$col_norm  = "#000000";
	$col_today = "#FF0000";

	$skipdays = date("w",mktime(0,0,0,$dmonth,1,$dyear)); // 0 for Sunday
	$countto  = date("d",mktime(0,0,0,$dmonth+1,0,$dyear));
	//print ("For month=$dmonth of $dyear: first=$skipdays, last=$countto today=$now_mon/$now_day\n");
	$monthN = date("F",mktime(0,0,0,$dmonth,1,$dyear));
	$dayN = array("0"=>"Sun", "1"=>"Mon", "2"=>"Tue", "3"=>"Wed", "4"=>"Thu", "5"=>"Fri", "6"=>"Sat");

	$html .= "<table width=\"$twid\" border=\"1\" align=\"center\" bgcolor=\"#DDFFDD\">\n";
	$html .= "  <tr height=\"40\" align=\"center\" bgcolor=\"#AAFFAA\">\n";
	$html .= "    <td width=\"$cwid\" align=\"center\"><font size=\"+2\"><b><a href=\"cal.php?id=$id&m=$pmonth\">&lt;&lt;</a></b></font></td>\n";
	$html .= "    <td align=\"center\" colspan=\"5\"><font size=\"+2\"><b>$monthN $dyear</b></font></td>\n";
	$html .= "    <td width=\"$cwid\" align=\"center\"><font size=\"+2\"><b><a href=\"cal.php?id=$id&m=$nmonth\">&gt;&gt;</a></b></font></td>\n";
	$html .= "  </tr>\n";
	$html .= "  <tr align=\"center\" bgcolor=\"#88FF88\">\n";
	for ($i=0; $i<=6; $i++) {
		$html .= "    <td width=\"$cwid\"><b>$dayN[$i]</b></td>\n";
	}
	$html .= "  </tr>\n";
	$cntD = 1;
	$r = 1;
	while ($cntD <= $countto) {
		$html .= "  <tr height=\"$rhei\">\n";
		for ($c=0; $c<=6; $c++) {
			if ($r == 1 && $c < $skipdays) {
				$html .= "    <td width=\"$cwid\"></td>\n";
			} else {
				if ($dmonth == $now_mon && $cntD == $now_day) { //it's today
					$foncol = $col_today;
					$brdcol = " bordercolor=\"$col_today\"";
				} else {
					$foncol = $col_norm;
					$brdcol = "";
				}
				$html .= "    <td width=\"$cwid\" valign=\"top\"$brdcol>\n";
				$html .= "      <p align=\"right\"><font size=\"+2\" color=\"$foncol\"><b>$cntD</b></font></p>\n";
				if (isset($monEvts[$cntD])) {
					$html .= $monEvts[$cntD];
				}
				$html .= "    </td>\n";
				$cntD++;
				if ($cntD > $countto) { break; }
			}
		}
		$r++;
		$html .= "  </tr>\n";
	}
	$html .= "</table>\n";

	return $html;
}
	

// MAIN

$now = localtime(time(),true);
$dmonth = $now[tm_mon]+1;
$dyear  = $now[tm_year]+1900;
$now_mon = $dmonth;
$now_day = $now[tm_mday];

// GET HTTP "GET" VARS

if ( isset($_GET['id']) ) {
        $id = $_GET['id'];
} else {
        $id = 3054;
}
$id=preg_replace("/[^0-9]/","",$id);  //get rid of anything but numbers
if ($id < 1 || $id > pow(2,17)) {
	die("ID is out of range.");
}

if ( isset($_GET['m']) ) {
        $gmonth = $_GET['m'];
	$gmonth=preg_replace("/[^0-9]/","",$gmonth);  //get rid of anything but numbers
	if ($gmonth < 1 || $gmonth > 12) {
		$gmonth = "";
	}
}
if ($gmonth > 0) { $dmonth = $gmonth; }

// SET UP TEMPLATE
                
$isMobile = ChkIsMobile();
$tpl = new HTML_Template_IT("templates");
$isMobile ? $template = "cal_mtmpl.html" : $template = "cal_tmpl.html";

$status = $tpl->loadTemplatefile($template, true, true);

$monEvts = "";
LoadEvents($dyear, $dmonth, "dob", "b.");
LoadEvents($dyear, $dmonth, "dod", "d.");

$cal_html = DrawCal($dyear, $dmonth, $now_mon, $now_day);


$tpl->setVariable("PersonID", $id);
$tpl->setVariable("MainCal", $cal_html);


$tpl->show();

?>
